<template>
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    okText="尾箱关闭"
  >
    <p-spin :spinning="confirmLoading">
      <p-form-model :model="form" ref="formRules" :rules="rules">
        <p-row type="flex" justify="space-between">
          <p-col :span="16">
            <p-form-model-item
              label="条码扫描"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <p-input
                placeholder="请输入条码"
                v-model.trim="form.sn"
              ></p-input>
            </p-form-model-item>
          </p-col>
        </p-row>
      </p-form-model>
      <p-table
        ref="table"
        size="middle"
        bordered
        :pagination="false"
        rowKey="id"
        :columns="columns"
        :scroll="tableScroll"
        :dataSource="dataSource"
        :loading="loading"
      />
    </p-spin>
  </p-modal>
</template>

<script>
import { httpAction, putAction, api } from "@/api/manage";
import { getAction, postAction } from "../../../../api/manage";
const columns = [
  {
    title: "序号",
    dataIndex: "",
    key: "rowIndex",
    width: 60,
    align: "center",
    customRender: function (t, r, index) {
      return parseInt(index) + 1;
    },
  },
  {
    title: "SN/PN",
    align: "center",
    dataIndex: "pnsn",
  },
  {
    title: "生产工单",
    align: "center",
    dataIndex: "productionNo",
  },
  {
    title: "产品编码",
    align: "center",
    dataIndex: "productCode",
  },
  {
    title: "产品描述",
    align: "center",
    dataIndex: "productName",
  },
  {
    title: "单位",
    align: "center",
    dataIndex: "unit",
  },
  {
    title: "数量",
    align: "center",
    dataIndex: "pnsnQty",
  },
];
export default {
  name: "",
  data() {
    return {
      form: {
        sn: "",
      },
      width: 800,
      columns: [],
      visible: false,
      confirmLoading: false,
      tableScroll: { x: 400, y: 400 },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 4 },
      },
      dataSource: [],
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 20 },
      },
      url: {
        getWaitForStorge: "/mes-manufacture/mfHandFilling/getWaitForStorage", //获得待包装列表
        closeContainer: "/mes-manufacture/mfContainer/closeContainer", //尾箱关闭
        packedInStorage: "/mes-manufacture/mfHandFilling/packedInStorage", //包装入库
      },
      rules: {},
      station: "",
      selectedRowKeys: [],
      selectionRows: [],
      loading: false,
    };
  },
  watch: {
    "form.sn": function (val) {
      this.getWaitForStorge(val);
    },
  },
  created() {
    this.columns = columns;
  },
  methods: {
    edit(station) {
      this.visible = true;
      this.station = station;
    },
    close() {
      this.visible = false;
      this.form.sn = "";
      this.dataSource = [];
    },
    handleOk() {
      if (!this.form.sn) return this.$message.warning("请先进行扫描");
      this.packedInStorage();
    },
    getWaitForStorge(barCode) {
      if (barCode) {
        this.loading = true;
        let params = {
          barCode,
          stationCode: this.station,
        };
        getAction(this.url.getWaitForStorge, params)
          .then((res) => {
            if (res.success) {
              let bool = this.dataSource.some(
                (item) => item.pnsn === res.data.child.pnsn
              );
              if (!bool) {
                this.dataSource.push(res.data.child);
              } else {
                this.$message.warning("该条码重复扫描");
              }
              if (
                this.dataSource.length ===
                res.data.horizontalNumber * res.data.stackingLayers
              ) {
                this.packedInStorage();
              }
            } else {
              this.$message.warning(res.msg);
            }
          })
          .finally(() => {
            this.loading = false;
          });
      }
    },
    //尾箱关闭
    closeContainer() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          postAction(this.url.closeContainer, {
            containerCode: this.formitem.containerCode,
          }).then((res) => {
            if (res.code === 0) {
              this.$message.success("尾箱关闭成功");
            }
          });
        }
      });
    },
    //包装入库
    packedInStorage() {
      let childs = this.dataSource.map((item) => {
        let { id, wipId, pnsn } = item;
        return { id, wipId, pnsn };
      });
      let params = {
        bindingState: 1,
        childs,
      };
      postAction(this.url.packedInStorage, params).then((res) => {
        if (res.code === 0) {
          this.$message.success("入库成功");
          this.dataSource = [];
          this.form.sn = "";
          this.$emit("refresh");
        } else {
          this.$message.warning(res.msg);
        }
      });
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectionRows = selectionRows;
      console.log(this.selectedRowKeys, this.selectionRows);
    },
    handleCancel() {
      this.close();
    },
  },
};
</script>

<style lang='scss' scoped>
</style>
